package org.zoomdev.zoom.common.utils;

import java.util.regex.Pattern;

public class ValidateUtils {
    public static final Pattern INT_PATTERN = Pattern.compile("^[+]?\\d+$");
    public static final Pattern FLOAT_PATTERN = Pattern.compile("^[-\\+]?\\d+(\\.\\d+)?$");
    static final Pattern VARIABLE_PATTERN = Pattern.compile("^[a-zA-Z_$][a-zA-Z_0-9$]+");

    /**
     * 是否是java变量名称，规则 含字母数字下划线$,必须以字母或下划线$开头
     *
     * @return
     */
    public static boolean isJavaVariableName(String src) {
        if (src == null) return false;
        return VARIABLE_PATTERN.matcher(src).matches();
    }

    /**
     * 是否是一个java类名称
     *
     * @param src
     * @return
     */
    public static boolean isJavaClassName(String src) {
        if (src == null) return false;
        if (src.isEmpty()) return false;
        if (src.endsWith(".")) return false;
        if (src.startsWith(".")) return false;
        String[] parts = src.split("\\.");
        if (parts.length == 0) return false;
        for (String string : parts) {
            if (string.isEmpty()) {
                return false;
            }
            if (!isJavaVariableName(string)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否是整数
     *
     * @param value
     * @return
     */
    public static boolean isInteger(String value) {
        if (value == null) return false;
        return INT_PATTERN.matcher(value).matches();
    }

    /**
     * 是否是浮点
     *
     * @param value
     * @return
     */
    public static boolean isFloat(String value) {
        if (value == null) return false;
        return FLOAT_PATTERN.matcher(value).matches();
    }

    public static boolean isNumber(String value) {
        if (value == null) return false;
        return isInteger(value) || isFloat(value);
    }
}
